package com.openbravo.pos.payment;

import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.util.AltEncrypter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class PaymentGatewayPlanetauthorize
  implements PaymentGateway
{
  private static final String ENDPOINTADDRESS = "https://secure.planetauthorizegateway.com/api/transact.php";
  private static final String OPERATIONVALIDATE = "sale";
  private static final String OPERATIONREFUND = "refund";
  private String m_sCommerceID;
  private String m_sCommercePassword;
  private boolean m_bTestMode;

  public PaymentGatewayPlanetauthorize(AppProperties paramAppProperties)
  {
    this.m_sCommerceID = paramAppProperties.getProperty("payment.commerceid");
    AltEncrypter localAltEncrypter = new AltEncrypter("cypherkey" + paramAppProperties.getProperty("payment.commerceid"));
    this.m_sCommercePassword = localAltEncrypter.decrypt(paramAppProperties.getProperty("payment.commercepassword").substring(6));
    this.m_bTestMode = Boolean.valueOf(paramAppProperties.getProperty("payment.testmode")).booleanValue();
  }

  public PaymentGatewayPlanetauthorize()
  {
  }

  public void execute(PaymentInfoMagcard paramPaymentInfoMagcard)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    try
    {
      localStringBuffer.append("username=");
      localStringBuffer.append(this.m_sCommerceID);
      localStringBuffer.append("&password=");
      localStringBuffer.append(this.m_sCommercePassword);
      localStringBuffer.append("&amount=");
      DecimalFormat localDecimalFormat = new DecimalFormat("0000.00");
      String str1 = localDecimalFormat.format(Math.abs(paramPaymentInfoMagcard.getTotal()));
      localStringBuffer.append(URLEncoder.encode(str1.replace(',', '.'), "UTF-8"));
      if (paramPaymentInfoMagcard.getTrack1(true) == null)
      {
        localStringBuffer.append("&ccnumber=");
        localStringBuffer.append(URLEncoder.encode(paramPaymentInfoMagcard.getCardNumber(), "UTF-8"));
        localStringBuffer.append("&ccexp=");
        localStringBuffer.append(paramPaymentInfoMagcard.getExpirationDate());
        String[] localObject = paramPaymentInfoMagcard.getHolderName().split(" ");
        localStringBuffer.append("&firstname=");
        if (localObject.length > 0)
          localStringBuffer.append(URLEncoder.encode(localObject[0], "UTF-8"));
        localStringBuffer.append("&lastname=");
        if (localObject.length > 1)
          localStringBuffer.append(URLEncoder.encode(localObject[1], "UTF-8"));
      }
      else
      {
        localStringBuffer.append("&track_1=" + URLEncoder.encode(paramPaymentInfoMagcard.getTrack1(true), "UTF-8"));
        localStringBuffer.append("&track_2=" + URLEncoder.encode(paramPaymentInfoMagcard.getTrack2(true), "UTF-8"));
      }
      if (paramPaymentInfoMagcard.getTotal() > 0.0D)
      {
        localStringBuffer.append("&type=");
        localStringBuffer.append("sale");
      }
      else
      {
        localStringBuffer.append("&type=");
        localStringBuffer.append("refund");
        localStringBuffer.append("&transactionid=");
        localStringBuffer.append(paramPaymentInfoMagcard.getTransactionID());
      }
      Object localObject = new URL("https://secure.planetauthorizegateway.com/api/transact.php");
      URLConnection localURLConnection = ((URL)localObject).openConnection();
      localURLConnection.setDoOutput(true);
      localURLConnection.setUseCaches(false);
      localURLConnection.setAllowUserInteraction(false);
      localURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      DataOutputStream localDataOutputStream = new DataOutputStream(localURLConnection.getOutputStream());
      localDataOutputStream.write(localStringBuffer.toString().getBytes());
      localDataOutputStream.flush();
      localDataOutputStream.close();
      BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream()));
      String str2 = localBufferedReader.readLine();
      paramPaymentInfoMagcard.setReturnMessage(str2);
      localBufferedReader.close();
      if (str2 == null)
      {
        paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), "Response empty.");
      }
      else
      {
        HashMap localHashMap = new HashMap();
        StringTokenizer localStringTokenizer = new StringTokenizer(str2, "?&");
        while (localStringTokenizer.hasMoreTokens())
        {
          String str3 = localStringTokenizer.nextToken();
          int i = str3.indexOf(61);
          if (i >= 0)
            localHashMap.put(URLDecoder.decode(str3.substring(0, i), "UTF-8"), URLDecoder.decode(str3.substring(i + 1), "UTF-8"));
          else
            localHashMap.put(URLDecoder.decode(str3, "UTF-8"), null);
        }
        if ("100".equals(localHashMap.get("response_code")))
          paramPaymentInfoMagcard.paymentOK((String)localHashMap.get("authcode"), (String)localHashMap.get("transactionid"), str2);
        else
          paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), (String)localHashMap.get("responsetext"));
      }
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymentexceptionservice"), localUnsupportedEncodingException.getMessage());
    }
    catch (MalformedURLException localMalformedURLException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymentexceptionservice"), localMalformedURLException.getMessage());
    }
    catch (IOException localIOException)
    {
      paramPaymentInfoMagcard.paymentError(AppLocal.getIntString("message.paymenterror"), localIOException.getMessage());
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.payment.PaymentGatewayPlanetauthorize
 * JD-Core Version:    0.6.0
 */